package com.test.fundible.fundible_01_launcher;

import static com.test.fundible.fundible_00_utils.FundibleUtil.startLoading;
import static com.test.fundible.fundible_00_utils.FundibleUtil.stopLoading;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.test.fundible.fundible_00_utils.FundibleConfigs;
import com.test.fundible.fundible_00_utils.FundibleUtil;
import com.test.fundible.fundible_03_login.LoginAc_fundibletivity;
import com.test.fundible.fundible_02_permission.Permission_fundibleActivity;
import com.test.fundible.fundible_00_analytics.fundibletimezone.FundibleTimeZoneUtil;
import com.test.fundible.fundible_00_analytics.FundibleUtil.FundibleAnalyticsUtil;
import com.test.fundible.R;
import com.test.fundible.fundible_04_home.MainAct_fundibleivity;
import com.test.fundible.fundible_03_login.Log_fundibleinInfo;
import com.test.fundible.fundible_00_http.OkHttpUtil;

import org.json.JSONException;
import org.json.JSONObject;


public class Launcher_fundibleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fundible_launcher);
        //tag2024
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        //启动页初始化
        findViewById(R.id.al_start).setOnClickListener(v -> start_fundibleProcess());

    }

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("LAUNCH","START",getString(R.string.local_config));
        start_fundibleProcess();
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("LAUNCH","END","");
    }

    private void start_fundibleProcess(){
        startLoading(this,"");
        //1.获取时区配置
        OkHttpUtil.getInstance_fundible().fetchTimeZoneConfig(
                FundibleTimeZoneUtil.INSTANCE.getTimeZoneID_fundible(),
                this::on_fundibleTZ_fundibleResponse
        );
    }

    public void on_fundibleTZ_fundibleResponse(){
        stopLoading();
        //2.检查权限
        if(!FundibleUtil.checkStartPermission_fundible(this)){
            Intent intent = new Intent(this, Permission_fundibleActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return;
        }

        startLoading(this,"");

        FundibleAnalyticsUtil.INSTANCE.init_fundible(Base_fundibleApplication.getInstant());

        OkHttpUtil.getInstance_fundible().fetchLauncherConfig(this::on_fundibleApp_fundibleShow_fundibleInfoResponse);
    }

    private void on_fundibleApp_fundibleShow_fundibleInfoResponse(){

        //自动登录
        if (FundibleUtil.didLogin_fundible()){
            try {
                JSONObject object = new JSONObject();
                object.put("token", FundibleUtil.getToken_fundible());
                object.put("userId", FundibleUtil.getUserId_fundible());
                object.put("type", FundibleConfigs.LOGIN_TYPE_OL_fundibleD);
                object.put("isAdSource", FundibleUtil.getIsAdSource_fundible() ? "YES":"NO");
                Log_fundibleinInfo.par_fundiblese(object);

                startLoading(this,"");

                FundibleAnalyticsUtil.INSTANCE.saveFirebaseAndFacebookS2SInfo_fundible();
                OkHttpUtil.getInstance_fundible().requestUsPrcs(this::on_fundibleUser_fundibleProcess_fundibleResponse);
                return;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }

        stopLoading();

        Intent intent = new Intent(this, LoginAc_fundibletivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
    }

    private void on_fundibleUser_fundibleProcess_fundibleResponse(){
        stopLoading();
        startActivity(new Intent(this, MainAct_fundibleivity.class));
        finish();
    }

}